package TakeAwaySystem;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/25 22:18:02
 */
public class Food {
    public String Name;
    public String foodExplain;
    public double foodPrice;
    public int businessId;
    public static final File file = new File("D:/TakeAwaySystem/Food.txt");
    public static final int FOODEXPLAIN = 2;
    public static final int FOODPRICE = 3;
    public Food(){
    }



    public Food(String name, String foodExplain, double foodPrice, int businessId) {
       // super(id, name);
        this.Name = name;
        this.foodExplain = foodExplain;
        this.foodPrice = foodPrice;
        this.businessId = businessId;
    }

    public String getFoodExplain() {
        return foodExplain;
    }

    public void setFoodExplain(String foodExplain) {
        this.foodExplain = foodExplain;
    }

    public double getFoodPrice() {
        return foodPrice;
    }

    public void setFoodPrice(double foodPrice) {
        this.foodPrice = foodPrice;
    }

    public int getBusinessId() {
        return businessId;
    }

    public void setBusinessId(int businessId) {
        this.businessId = businessId;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public static void ListToFoodFile(List<Food> list, File file) {
        // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for (int i = 0; i < N; i++) {
                Food user = (Food) list.get(i);
                StringBuilder sbr = new StringBuilder();
                //sbr.append(user.getId() + " ");
                sbr.append(user.getName() + " ");
                sbr.append(user.getFoodExplain() + " ");
                sbr.append(user.getFoodPrice() + " ");
                sbr.append(user.getBusinessId() + " ");
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }


    public static List<Food> fileToList(File file) {
        List<Food> list = new ArrayList<>();
        //File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while (true) {
                String str = br.readLine();
                if (str != null) {
                    String[] arr = str.split(" ");
                    Food food = new Food();
                    // food.setId(Integer.valueOf(arr[0]));
                    food.setName(arr[0]);
                    food.setFoodExplain(arr[1]);
                    food.setFoodPrice(Double.valueOf(arr[2]));
                    food.setBusinessId(Integer.valueOf(arr[3]));
                    list.add(food);
                } else {
                    break;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }

    @Override
    public String toString() {
        return "Food{" +
                "Name='" + Name + '\'' +
                ", foodExplain='" + foodExplain + '\'' +
                ", foodPrice=" + foodPrice +
                ", businessId=" + businessId +
                '}';
    }
}
